package com.tenentia.pcmps.swm.data.implementation;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.tenentia.pcmps.swm.Constants;
import com.tenentia.pcmps.swm.data.DataAccessException;
import com.tenentia.pcmps.swm.data.beans.State;
import com.tenentia.pcmps.swm.data.interfaces.StatesDAO;
import com.tenentia.pcmps.swm.data.util.IBatisUtilities;

/**
 * 
 * @author tEnEntia, S.L.N.E.
 * 
 */
public class StatesDAOImpl implements StatesDAO {

	private static final Logger LOGGER = Logger.getLogger(StatesDAOImpl.class.getPackage().getName());
	
	public StatesDAOImpl() {
		super();
	}

	@Override
	public State get(State state) throws DataAccessException {
		try {
			state = (State) IBatisUtilities.getSqlMapClient().queryForObject("getState", state.getInternalID());
			LOGGER.debug(state);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}
		return state;
	}

	@Override
	public List<State> getAvailableStates() throws DataAccessException {
		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		List<State> methodResult;

		try {
			List<?> tmpList = IBatisUtilities.getSqlMapClient().queryForList("getAvailableStates");

			if (null == tmpList || tmpList.size() == 0) {
				LOGGER.debug("No results...");
				methodResult = new ArrayList<State>();
			} else {
				methodResult = new ArrayList<State>();
				for (int i = 0; i < tmpList.size(); i++) {
					State tmpState = (State) tmpList.get(i);
					methodResult.add(tmpState);
					LOGGER.debug("tmpState: " + tmpState);
				}
			}
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new DataAccessException(e);
		}

		return methodResult;
	}

}
